package com.example.CarManagement.config.SecurityConfig;

import com.alibaba.fastjson2.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {


        HashMap<String, Object> result = new HashMap<>();
        result.put("code", -1);

        // 失败信息
        result.put("message", "访问此资源需要完全身份验证（登录~~~）");

        String json = JSON.toJSONString(result);

        // 认证成功
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
    }
}
